package com.atstudy.lambda;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/1/4 14:09
 * @copyright: 博为峰
 */
public class MethodRef {
    public static void main(String[] args) {
//        m1();
//        m2();
//        m3();
        Supplier<Student> supplier = new Supplier<>() {
            @Override
            public Student get() {
                return new Student();
            }
        };
        /*
             public Student() {
             }
        */
        supplier = Student::new;
    }

    private static void m3() {
        Function<Double, Long> function = new Function<>() {
            @Override
            public Long apply(Double aDouble) {
                return Math.round(aDouble);
            }
        };

        function = aDouble -> Math.round(aDouble);

        /*
           Math
           public static long round(double a) {
        */
        function = Math::round;

        System.out.println(function.apply(3.56));
    }

    //类::静态方法名
    private static void m2() {
        Comparator<Integer> comparator = new Comparator<>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        };
        comparator = (o1, o2) -> o1 - o2;

        /*
            Integer
            public static int compare(int x, int y) {
                return (x < y) ? -1 : ((x == y) ? 0 : 1);
            }
         */

        comparator = Integer::compare;
//        comparator = Math::round;
//        comparator = System.out::println;

        int i = comparator.compare(9, 6);
        System.out.println(i);
    }

    //对象::实例方法名
    private static void m1() {
        Consumer<String> consumer = new Consumer<>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

        consumer = s -> System.out.println(s);

//        PrintStream out = System.out;
//        public void println(String x)

        consumer = System.out::println;

        consumer.accept("29期");
    }
}
